<template>
  <el-dialog
    :title="'选择数据表'"
    
    :close-on-click-modal="false"
    :visible.sync="visible">
    <!-- <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="表名" prop="tableName">
      <el-input v-model="dataForm.tableName" placeholder="表名"></el-input>
    </el-form-item>
    <el-form-item label="业务包id外键" prop="bPId">
      <el-input v-model="dataForm.bPId" placeholder="业务包id外键"></el-input>
    </el-form-item>
    <el-form-item label="列名，*代表所有列" prop="columnName">
      <el-input v-model="dataForm.columnName" placeholder="列名，*代表所有列"></el-input>
    </el-form-item>
    </el-form> -->

   <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <!-- <el-form-item label="数据集名" prop="dataName">
      <el-input v-model="dataForm.dataName" placeholder="表名"></el-input>
    </el-form-item> -->
    </el-form> 

     <el-transfer  :titles="['数据库表', '已选择表']" :right-default-checked="[4]" v-model="selectedTables" :data="tableNames"></el-transfer>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
       const generateData = _ => {
        const data = [];
        for (let i = 1; i <= 15; i++) {
          data.push({
            key: i,
            label: `备选项 ${ i }`
          });
        }
        return data;
      };
      return {
        visible: false,
        dataForm: {
          id: 0,
          tableName: '',
          bPId: '',
          columnName: ''
        },
        dataRule: {
          tableName: [
            { required: true, message: '表名不能为空', trigger: 'blur' }
          ],
          bPId: [
            { required: true, message: '业务包id外键不能为空', trigger: 'blur' }
          ],
          columnName: [
            { required: true, message: '列名，*代表所有列不能为空', trigger: 'blur' }
          ]
        },
        tableNames: [],
        selectedTables: [],
        bussinessId: '',
        transfers: []
      }
    },
    methods: {
      getSelectedTables(dataid){
        this.selectedTables=[];
        this.$http({
          url: this.$http.adornUrl(`/report/datapackage/tabls/${dataid}`),
          method: 'get',
          params: this.$http.adornParams(
          )
        }).then(({data}) => {
              //this.tableNames=data.tableNames;
          for(var i=0;i<data.selectedTableNames.length;i++){
         //   this.selectedTables.push(data.selectedTableNames[i].key);
           this.selectedTables.push(this.tableNames.find(item=>{
              return item.label==data.selectedTableNames[i].label;
            }).key)

           }
        })
       // console.log(123);
      },
      getAllTables(bussinessId){
        this.$http({
          url: this.$http.adornUrl(`/report/datapackage/allTables/${bussinessId}`),
          method: 'get',
          params: this.$http.adornParams(
          )
        }).then(({data}) => {
              this.tableNames=data.tableNames;
              console.log("222222222222"+this.tableNames.length);
          if (this.dataForm.id) {
              this.getSelectedTables(this.dataForm.id);
            }
        })
      

      },

      init (id,bussinessId) {
        this.bussinessId=bussinessId;
        this.visible = true
         this.dataForm.id = id || 0
         this.getAllTables(bussinessId);
       
        // this.dataForm.id = id || 0
        // this.visible = true
        // this.$nextTick(() => {
        //   this.$refs['dataForm'].resetFields();
        //  this.getAllTables(bussinessId);
          // if (this.dataForm.id) {
          //   this.getSelectedTables(this.dataForm.id);
          //   this.$http({
          //     url: this.$http.adornUrl(`/report/datapackage/info/${this.dataForm.id}`),
          //     method: 'get',
          //     params: this.$http.adornParams()
          //   }).then(({data}) => {
          //     if (data && data.code === 0) {
          //       this.dataForm.tableName = data.dataPackage.tableName
          //       this.dataForm.bPId = data.dataPackage.bPId
          //       this.dataForm.columnName = data.dataPackage.columnName
          //     }
          //   })
          // }
        // })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
           this.transfers=[];
          for(var i=0;i<this.selectedTables.length;i++){
           this.transfers.push( this.tableNames.find(item=>{
                console.log("tableNames: "+item.key)
                  console.log("selectedTables: "+this.selectedTables[i])
                return item.key==this.selectedTables[i];
            }))
          }



          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/report/datapackage/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'bussinessId': this.bussinessId,
                'userId': this.$store.state.user.id,
                'transfers': this.transfers
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
